package com.itheima.controller;

import com.itheima.pojo.Result;
import com.itheima.pojo.User1;
import com.itheima.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/users")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping
    public Result selectAll() {

        List<User1> user1List = userService.selectAll();

        //通过打印userService对象，可以观察出是否使用了代理
        System.out.println(userService.getClass());/*class com.itheima.service.impl.UserServiceImpl$$EnhancerBySpringCGLIB$$1e541cfb 这是原来的UserServiceImpl类的代理类对象,也就是说自动注入的那个userService实现类对象是原来的UserService接口的代理类对象,selectAll调用的也将是切面类增强后的方法*/

        return Result.success(user1List);
    }

    @GetMapping("/{id}")
    public Result selectById(@PathVariable Integer id) {

        User1 user1 = userService.selectById(id);
        return Result.success(user1);
    }


    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable Integer id) {

        userService.deleteById(id);
        return Result.success("删除成功!");
    }
}
